package com.me.web;

import com.me.dao.impl.PetDao;
import com.me.dao.impl.UserDao;
import com.me.domain.Pet;
import com.me.domain.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.util.List;

@WebServlet("/custom")
public class CustomerServlet extends BaseServlet {
    @Override
    protected void doGet() throws ServletException, IOException {
        String m = getParam("m");
        if (m ==null){
            UserDao userDao = new UserDao();
            List<User> users = userDao.getAll();
            setReqAttr("users",users);
            forward("/admin/userManage.jsp");
        }else if (m.equals("detail")){
            showDetail();
        } else if (m.equals("edit")) {
            edit();
        }
    }

    private void edit() throws ServletException, IOException {
        UserDao userDao = new UserDao();
        User user = userDao.getById(getIntParam("id"));
        setReqAttr("user",user);
        forward("/admin/userEdit.jsp");
    }

    private void showDetail() throws ServletException, IOException {
        UserDao userDao = new UserDao();
        PetDao petDao = new PetDao();
        int ownerId=Integer.valueOf(getParam("cid"));
        User user = userDao.getById(ownerId);
        List<Pet>pets=petDao.getPetsByOwnerId(ownerId);
        user.setPets(pets);
        setReqAttr("user",user);

    }
    @Override
    protected void doPost() throws ServletException, IOException {
        String m = getParam("m");
        if ("search".equals(m)){
            search();
        }else if ("add".equals(m)){
            add();
        } else if ("edit".equals(m)) {
            update();
        } else if ("del".equals(m)) {
            del();
        }else if ("repwd".equals(m)){
            repwd();
        }
    }

    private void del() throws ServletException, IOException {
        User user = new User();
        user.setId(getIntParam("id"));
        UserDao userDao = new UserDao();
        userDao.del(user);
        setReqAttr("msg","删除成功");
        forward("./custom");
    }

    private void add() throws ServletException, IOException {
        User user = new User();
        user.setName(getParam("name"));
        user.setPwd("123456");
        user.setRole("customer");
        user.setTel(getParam("tel"));
        user.setAddress(getParam("address"));
        UserDao userDao = new UserDao();
        userDao.save(user);
        setReqAttr("msg","添加用户成功");
        forward("./custom");
    }

    private void update() throws ServletException, IOException {
        User user = new User();
        user.setId(getIntParam("id"));
        user.setName(getParam("name"));
        user.setPwd("123456");
        user.setRole("customer");
        user.setTel(getParam("tel"));
        user.setAddress(getParam("address"));
        UserDao userDao = new UserDao();
        userDao.update(user);
        setReqAttr("msg","更改信息成功");
        if (getSesUser().getRole().equals("admin")) {
            forward("./custom");
        } else {
            forward("/personal.jsp");
        }
    }

    private void repwd() throws ServletException, IOException {
        User user = new User();
        user.setId(getIntParam("id"));
        user.setPwd(getParam("pwd"));
        UserDao userDao = new UserDao();
        userDao.repwd(user);
        setReqAttr("msg","更改密码成功");
        forward("./index.jsp");
        clearSes();
    }

    private void search() throws ServletException, IOException {
        UserDao userDao = new UserDao();
        List<User>users = userDao.searchCustomer(getParam("customerName"));
        if (users.size()==0){
            setReqAttr("msg","没有找到相关客户信息");
            forward("/admin/userManage.jsp");
        }else {
            setReqAttr("users",users);
            forward("/admin/userManage.jsp");
        }
    }
}
